package com.example.cyh.bczapp.Code.Activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.cyh.bczapp.Code.BLL.CommonBll;
import com.example.cyh.bczapp.Code.BLL.UserBLL;
import com.example.cyh.bczapp.Code.Model.UserInfEntity;
import com.example.cyh.bczapp.R;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegisterActivity extends Activity {
    private String Code;
    private Handler handler = new Handler();
    private String outtime;
    private String id;
    private String pwd;
    private View.OnClickListener send = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String Email = ((TextView) findViewById(R.id.email)).getText().toString();
            if (Email == "") {
                Toast.makeText(RegisterActivity.this, "请先填写邮箱!", Toast.LENGTH_SHORT).show();
                return;
            }
            //验证邮箱
//            Pattern pattern = Pattern.compile("\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}");
//            Matcher matcher = pattern.matcher(Email);
//            if(matcher.matches())
//            {
//                Toast.makeText(RegisterActivity.this, "邮箱格式错误!", Toast.LENGTH_SHORT).show();
//                return;
//            }

            new Thread(new Runnable() {
                @Override
                public void run() {
                    CommonBll cb = new CommonBll();
                    Code = cb.Sendcode(((TextView) findViewById(R.id.email)).getText().toString());
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            if (Code == "error") {
                                Toast.makeText(RegisterActivity.this, "邮箱没有发送成功!", Toast.LENGTH_SHORT).show();
                                Code = "";
                                return;
                            } else {
                                Toast.makeText(RegisterActivity.this, "邮件已发送!", Toast.LENGTH_SHORT).show();
                            }
                            findViewById(R.id.bt_get).setOnClickListener(null);
                            AfterSend();
                        }
                    });
                }
            }).start();

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        findViewById(R.id.bt_get).setOnClickListener(send);
        findViewById(R.id.bt_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        UserInfEntity userInfEntity = new UserInfEntity();
                        id = ((TextView) (findViewById(R.id.id))).getText().toString();
                        pwd = ((TextView) (findViewById(R.id.pwd))).getText().toString();
                        String Codestr = ((TextView) (findViewById(R.id.code))).getText().toString();
                        String Email = ((TextView) (findViewById(R.id.email))).getText().toString();
                        if (id != "" && pwd != "" && Codestr != "" && Email != "") {
                            if (!Code.equals(Codestr)) {
                                Toast.makeText(RegisterActivity.this, "验证码错误!", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            userInfEntity.U_LoginId = id;
                            userInfEntity.U_Pwd = pwd;
                            userInfEntity.U_Email = Email;
                            Date date = new Date();
                            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            userInfEntity.U_RegisterTime = format.format(date);
                            userInfEntity.U_Grade = "0";
                            userInfEntity.U_WorkDays = "0";
                            userInfEntity.U_Name = "cyhtest";
                            UserBLL ub = new UserBLL();
                            Looper.prepare();
                            if (ub.AddUser(userInfEntity)) {
                                Toast.makeText(RegisterActivity.this, "注册成功!", Toast.LENGTH_SHORT).show();
                                Login();
                            } else {
                                id="";
                                pwd="";
                                Code="";
                                Toast.makeText(RegisterActivity.this, "注册失败!", Toast.LENGTH_SHORT).show();
                            }
                            Looper.loop();
                        }
                    }
                }).start();
            }
        });
    }
    private void AfterSend() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 120; i > 0; i--) {
                    try {
                        Thread.sleep(1000);
                    } catch (Exception ex) {
                    }
                    outtime = String.valueOf(i);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            ((TextView) findViewById(R.id.bt_get)).setText("验证码失效还剩:" + String.valueOf(outtime) + "秒");
                        }
                    });
                }
                ((TextView) findViewById(R.id.bt_get)).setText("获取验证码");
                findViewById(R.id.bt_get).setOnClickListener(send);
            }
        }).start();
    }
    private void Login(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                ///写入文件系统
                SharedPreferences.Editor editor=getSharedPreferences("Logindata",MODE_PRIVATE).edit();
                editor.putString("id",id);
                editor.putString("pwd",pwd);
                editor.apply();
                ///写入文件系统
                UserBLL ub=new UserBLL();
                UserInfEntity user=ub.Login(id,pwd);
                if(user!=null) {
                    Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                    intent.putExtra("user", user);
                    RegisterActivity.this.startActivity(intent);
                    finish();
                }
               // Toast.makeText(RegisterActivity.this,user!=null?"登陆成功":"登陆失败", Toast.LENGTH_SHORT).show();
               // finish();
            }
        }).start();
    }
}
